/*
 * Created by Huang 嵘才 on 18-2-7 上午11:22
 * Copyright (c) 2018. All rights reserved.
 * Last modified 18-2-6 下午4:16
 */

package io.esirong.image;

import android.content.Context;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;

import io.esirong.image.fresco.FrescoImageLoader;
import io.esirong.image.gilde.GlideImageLoader;


/**
 * 实现整个图片加载架构的管理类，用于对外提供图片加载服务和图片加载框架的替换
 * Created by 黄嵘才(HuangRc) on 2018/2/1.
 */

public class ImageLoaderStrategyManager implements ImageLoaderStrategy {

    private static final ImageLoaderStrategyManager INSTANCE = new ImageLoaderStrategyManager();
    /**
     * 默认的图像加载配置
     */
    static ImageLoaderOptions OPTIONS = LoaderOptions.OPTIONS_DEFAULT;
    private ImageLoaderStrategy glideImageLoader;
    private ImageLoaderStrategy frescoImageLoader;

    private ImageLoaderStrategyManager() {
        //默认使用Glide
        glideImageLoader = GlideImageLoader.get();
        frescoImageLoader = FrescoImageLoader.get();
    }

    public static ImageLoaderStrategyManager getInstance() {
        return INSTANCE;
    }

    /**
     * 可实时替换图片加载框架策略
     */

    public void setFrescoImageLoader(ImageLoaderStrategy loader) {
        if (loader != null) {
            frescoImageLoader = loader;
        }
    }

    @Override
    public void init(Context context) {
        frescoImageLoader.init(context);
        glideImageLoader.init(context);
    }

    @Override
    public void showImage(View v, String url, ImageLoaderOptions options) {
        if (v instanceof SimpleDraweeView) {
            frescoImageLoader.showImage(v, url, options);
        } else {
            glideImageLoader.showImage(v, url, options);
        }

    }

    @Override
    public void showImage(View v, int drawable, ImageLoaderOptions options) {
        if (v instanceof SimpleDraweeView) {
            frescoImageLoader.showImage(v, drawable, options);
        } else {
            glideImageLoader.showImage(v, drawable, options);
        }

    }

    @Override
    public void showImage(View v, byte[] imageBytes, ImageLoaderOptions options) {

    }
}
